﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="UserNews.aspx.cs" Inherits="ncp.Web.cms.work.NewsManager.UserNews" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title></title>
    <link rel="stylesheet" type="text/css" href="../../themes/default/easyui.css" />
    <link rel="stylesheet" type="text/css" href="../../themes/icon.css" />
    <link href="../../css/base.css" rel="stylesheet" type="text/css" />
	<script type="text/javascript" src="../../js/jquery-1.7.1.min.js"></script>
	<script type="text/javascript" src="../../js/jquery.easyui.min.js"></script>
	<script type="text/javascript" src="../../../js/common.js"></script>
</head>
<body style="margin:0; padding:0px;">
<form id="form1" runat="server">
    <div id="rightlayout" class="easyui-layout"  style="width:100%; height:498px;">
	    <div id="west" region="west" split="true" title="新闻类别" style="width:220px;padding:1px;overflow:auto;">
	        <div class="toolbar" style="background-color:#efefef">
	        </div>
            <div id="divleft"></div>
	    </div>
	    <div id="east" region="center" title="新闻资料">
	        <div class="toolbar" style="background-color:#efefef">
		        <a href="#" class="easyui-linkbutton" plain="true" iconCls="icon-remove" onclick="onDeleteNews();">删除</a>
		        <a href="#" class="easyui-linkbutton" plain="true" iconCls="icon-edit" onclick="onEditFlag();">自定义属性</a>
		        <a href="#" class="easyui-linkbutton" plain="true" iconCls="icon-reload" onclick="onSetNewsStatus()">禁用/启用</a>&nbsp;
                <span>查询条件:</span>
                <select id="ddlQuery">
                    <option value="News_Title">文章标题</option>
                    <option value="News_Flag">自定义属性</option>
                    <option value="News_Date">更新时间</option>
                    <option value="News_times">点击次数</option>
                    <option value="User_Name">发布人</option>
                </select>
                <select name="ddlOperator" id="ddlOperator">
	                <option value="like">包含</option>
	                <option value="=">等于</option>
	                <option value=">">大于</option>
	                <option value="&lt;">小于</option>
                </select>
                <input name="txtQuery" type="text" id="txtQuery" style="width:80px;" />
    	        <a href="#" class="easyui-linkbutton" plain="true" iconCls="icon-search" onclick="onSearch()">查询</a>
	        </div>
            <div id="divright">    
            </div>
	    </div>
	    <div id="flagbox" class="mod-content">
	    <table style="margin-top: 10px;">
	        <tr>
                    <td class="mod-label"> <label for="newsTitle">自定义属性:</label></td>
                    <td align="left" id="Td1">
            	        <input type="checkbox" name="flags" id="flagsh" value="h"/>头条[h]
            	        <input  type="checkbox" name="flags" id="flagsc" value="c"/>推荐[c]
            	        <%--<input  type="checkbox" name="flags" id="flagsf" value="f"/>幻灯[f]--%>
            	        <input  type="checkbox" name="flags" id="flagsa" value="a"/>特荐[a]          
            	    </td>
                </tr>
                <tr>
                    <td colspan="2" align="center">
	            <a href="#" class="easyui-linkbutton" onclick="onSetFlags();">确定</a>&nbsp;&nbsp;
	            <a href="#" class="easyui-linkbutton" onclick="onCloseFlag();">关闭</a>
                    </td>
                </tr>
	    </table>
	</div>
	</div>
	<div class="previewbox">
	    <span title="关闭" onclick="hidePreview()" class="imgdelete"></span>
	    <img  src=""/>
	</div>
	</form>
</body>
</html>
<script type="text/javascript">
    $(function() { //初始化
        LoadNewsClass();
        initDialog();
    });

 
    
    //初始化对话框
    function initDialog() {
        $('#divNewsInfoBox').dialog({
            title: '商品资料',
            modal: true,
            width: $(window).width(),
            height: $(window).height(),
            left: 0,
            top: 0,
            padding: 10,
            closed: true,
            draggable: false,
                closable:false
        });
        $('#flagbox').dialog({
            title: '自定义属性',
            modal: true,
            width: 370,
            height: 130,
            left: 100,
            top: 150,
            padding: 10,
            closed:true
        });
    }
    
    //装载商品类别视图
    function LoadNewsClass() {
        var response = ncp.Web.cms.work.NewsManager.UserNews.ShowNewsClassTree();
        if (response.value != null) {
            var jsonOBJ = eval(response.value)[0];
            if (jsonOBJ.isreload) {
                $("#divleft").html(jsonOBJ.msg);
            } else
                alert(jsonOBJ.msg);
        }
    }

    //关闭商品类别对话框
    function onClose() {
        $('#divNewsClassBox').dialog("close");
    }
    function onCloseFlag() {
        $('#flagbox').dialog("close");
    }

    //得到本节点和它的子节点
    function getChildren() {
        var node = $('#tt1').tree('getSelected');
        if (node == null) return "";

        var s = node.id;
        var children = $('#tt1').tree('getChildren', node.target);

        for (var i = 0; i < children.length; i++) {
            s +=','+  children[i].id ;
        }
        return s;
    }

</script>

<%--商品信息--%>
<script type="text/javascript">
    var pageobj = new Object();

    $(function() {
        initTree();
        initPageObj();
        initDatagrid();
        LoadNewsInfo();
        initPager();
    });

    //初始化树
    function initTree() {
        $('#tt1').tree({
            onClick: function(node) {
                onSearch();
            }
        });
    }    
    
    //初始化页码对象
    function initPageObj() {
        pageobj.currentpageindex = 1;
        pageobj.pagesize = 10;
        pageobj.sorting = "News_Date";
        pageobj.sortdir = "desc";
        pageobj.currentwhere = GetstrWhere();
        pageobj.recordcount = GetRecordCount();
    }

    //初始化表格
    function initDatagrid() {
        $('#divright').datagrid({
            height: $(window).height() - 60,
            nowrap: true,
            striped: true,
            singleSelect: true,
            sortName: 'News_Date',
            sortOrder: 'desc',
            remoteSort: false,
            idField: 'News_Id',
            pagination: true,
            rownumbers: true,
            columns: [[
					{ field: 'News_Id', title: '文章编号', hidden: 'true', width: 80 },
					{ field: 'News_Title', title: '文章标题', sortable: 'true', width: 360 },
					{ field: 'News_Picture', title: '缩略图', sortable: 'false', width: 40, align: 'center',
					    formatter: function(val, rec) {
					        if (val != "") {
					            return "<img class=\"imgPreview\" onclick=\"imgPreView(this)\" src=\"/cms/images/pic.png\" title=\"预览\" titleimg=\"" + val + "\" style=\"cursor: pointer\">";
					        } else {
					            return "无";
					        }
					    }
					},
					{ field: 'News_Flag', title: '自定义属性', sortable: 'false', width: 100,
					    formatter: function(val, rec) {
					    var str = "<span style=\"color:red;\">";
					        if (val!=null) {
					            if (val.indexOf('h') != -1) {
					                str += "[头条]";
					            }
					            if (val.indexOf('c') != -1) {
					                str += "[推荐]";
					            }
					            if (val.indexOf('f') != -1) {
					                str += "[幻灯]";
					            }
					            if (val.indexOf('a') != -1) {
					                str += "[特荐]";
					            }
					        }
					        
					        str += "</span>";
					        return str;
					    }
					},
					{ field: 'News_Date', title: '更新时间', sortable: 'true', width: 130 },
					{ field: 'NC_Name', title: '文章类别', sortable: 'true', width: 80, align: 'center' },
					{ field: 'News_times', title: '点击', sortable: 'true', width: 60, align: 'center' },
					{ field: 'User_Name', title: '发布人', sortable: 'true', width: 60, align: 'center' },
					{ field: 'News_Status', title: '状态', sortable: 'true', width: 60, align: 'center',
					    styler: function(value, row, index) {
					        if (value == '0') {
					            return 'color:red;font-weight:bold;';
					        }
					    },
					    formatter: function(val, rec) {
					        if (val == '0') {
					            return "禁用";
					        } else {
					        return "启用";
					        }
					    }
					}
				]]
        });
    }

    function imgPreView(dom) {
        $(dom).click(function(e) {
            $(".previewbox img").attr("src", $(dom).attr("titleimg"));
            $(".previewbox").css({ "top": e.pageY+10, "left": e.pageX-200 });
            $(".previewbox").fadeIn();
        });
    }
    function hidePreview() {
        $('.previewbox').fadeOut();
    }

    function initPager() {
        var p = $('#divright').datagrid('getPager');
        $(p).pagination({
            total: pageobj.recordcount,
            pageSize: pageobj.pagesize,
            pageList: [10, 15, 20, 25, 30],
            showRefresh: true,
            showPageList:true,
            beforePageText: '第',
            afterPageText: '页 共 {pages} 页',
            displayMsg: '当前显示 {from} - {to} 条记录 共 {total} 条记录',
            onSelectPage: function(pageNumber, pageSize) {
                $(this).pagination('loading');
                onShowPageNo(pageNumber, pageSize);
                $(this).pagination('loaded');
            }
        });
    }
    
    //翻页
    function onShowPageNo(pageNumber, pageSize) {
        pageobj.pagesize = pageSize;
        pageobj.currentpageindex = pageNumber;
        LoadNewsInfo();
        initPager();
        return false;
    }

    //得到条件
    function GetstrWhere() {
        var myWhere = "Client_Id>=1000";
        var nc_code = getChildren();
        if (nc_code != "") myWhere += " and NC_Code in ('" + nc_code + "')";
        var response = ncp.Web.cms.work.NewsManager.UserNews.GetstrWhere($("#ddlQuery").val(), $("#ddlOperator").val(), $.trim($("#txtQuery").val()));
        if (response.value != null) {
            return myWhere + " and " + response.value;
        }
        else
            return myWhere;
    }

    //得到总记录数
    function GetRecordCount() {
        var response = ncp.Web.cms.work.NewsManager.UserNews.GetRecordCount(pageobj.currentwhere);
        if (response.value != null) {
            return response.value;
        }
        else
            return 0;
    }

    //查询
    function onSearch() {
        pageobj.currentpageindex = 1;
        pageobj.currentwhere = GetstrWhere();
        pageobj.recordcount = GetRecordCount();
        LoadNewsInfo();
        initPager();
        return false;
    }

    //装载商品信息视图
    function LoadNewsInfo() {
        var jsonStr = $.objectToJson(pageobj);
        var response = ncp.Web.cms.work.NewsManager.UserNews.BindTable(jsonStr);
        if (response.value != null) {
            var data = eval(response.value);
            $('#divright').datagrid('loadData', data);
        }
    }


    //获取选中的flag
    function getFlag() {
        var flags = "";
        $("#flag input").each(function() {
            if ($(this).attr("checked")) {
                flags +=","+ $(this).val();
            }
        });
        if (flags!="") {
            flags = flags.substring(1);
        }
        return flags;
    }
    
    //保存新闻
    function onSaveNews() {
        var obj = new Object();
        obj.News_Id = $("#newsId").val();
        obj.News_Title = $("#newsTitle").val();
        obj.NC_Code = $('#tt1').tree('getSelected').id;
        obj.description = $("#description").val();
        obj.News_Flag = getFlag();
        obj.News_Picture = $("#imageList li img").attr("src") == undefined ? "" : $("#imageList li img").attr("src");
        obj.News_Note = editor.getContent();
        var response = ncp.Web.cms.work.NewsManager.UserNews.OnSave($.objectToJson(obj));
        
        if (response.value != "") {
            var jsonOBJ = eval(response.value)[0];
            alert(jsonOBJ.msg);
            if (jsonOBJ.isreload) {
                onSearch();
                onCloseNewsWindow();
                emptyData();
            }
        }
    }

    //禁用启用文章
    function onSetNewsStatus() {
        var selected = $('#divright').datagrid('getSelected');
        if (selected) {
            var newsId = selected.News_Id;
            var status = selected.News_Status == 0 ? 1: 0;
            var response = ncp.Web.cms.work.NewsManager.UserNews.SetNewsStatus(newsId,status);
            if (response.value != "") {
                var jsonOBJ = eval(response.value)[0];
                alert(jsonOBJ.msg);
                if (jsonOBJ.isreload) {
                    LoadNewsInfo();
                }
            }
        }
        else
            alert("请先选择一条记录！");
    }
    function onEditFlag() {
        $("#flagbox input").removeAttr("checked");
        var selected = $('#divright').datagrid('getSelected');
        if (selected) {
            $('#flagbox').dialog('setTitle', '自定义属性');
            $('#flagbox').dialog("open");
            var flag = selected.News_Flag;
            if (flag != null) {
                if (flag.indexOf("h") != -1) {
                    $("#flagsh").attr("checked", true);
                }
                if (flag.indexOf("c") != -1) {
                    $("#flagsc").attr("checked", true);
                }
                if (flag.indexOf("f") != -1) {
                    $("#flagsf").attr("checked", true);
                }
                if (flag.indexOf("a") != -1) {
                    $("#flagsa").attr("checked", true);
                }
            }
        }
        else
            alert("请先选择一条记录！");
        return false;
    }

    function onSetFlags() {
        var selected = $('#divright').datagrid('getSelected');
        if (selected) {
            var flags = "";
            $("#flagbox input").each(function() {
                if ($(this).attr("checked")) {
                    flags += "," + $(this).val();
                }
            });
            if (flags != "") {
                flags = flags.substring(1);
            }
            var newsId = selected.News_Id;
            var response = ncp.Web.cms.work.NewsManager.UserNews.SetFlags(newsId, flags);
            if (response.value != "") {
                var jsonOBJ = eval(response.value)[0];
                alert(jsonOBJ.msg);
                if (jsonOBJ.isreload) {
                    onCloseFlag();
                    LoadNewsInfo();
                }
            }
        }
        else
            alert("请先选择一条记录！");
        return false;
    }

    //编辑商品
    function onEditNews() {
        var selected = $('#divright').datagrid('getSelected');
        if (selected) {
            $('#divNewsInfoBox').dialog('setTitle', '编辑新闻');
            $('#divNewsInfoBox').dialog("open");
            $("#newsId").val(selected.News_Id);
            $("#newsTitle").val(selected.News_Title);
            $("#description").val(selected.News_Description);
            editor.setContent(selected.News_Note);
            if (selected.News_Picture!="") {
                appendList(selected.News_Picture);
            }
            var flag = selected.News_Flag;
            if (flag != null) {
                if (flag.indexOf("h") != -1) {
                    $("#flagsh").attr("checked", true);
                }
                if (flag.indexOf("c") != -1) {
                    $("#flagsc").attr("checked", true);
                }
                if (flag.indexOf("f") != -1) {
                    $("#flagsf").attr("checked", true);
                }
                if (flag.indexOf("a") != -1) {
                    $("#flagsa").attr("checked", true);
                }
            }
        }
        else
            alert("请先选择一条记录！");
        return false;
    }

    //删除商品
    function onDeleteNews() {
        var selected = $('#divright').datagrid('getSelected');
        if (selected) {
            if (confirm("您确定要删除吗？")) {
                var response = ncp.Web.cms.work.NewsManager.UserNews.DeleteNewsInfo(selected.News_Id);
                if (response.value != null) {
                    var jsonOBJ = eval(response.value)[0];
                    if (jsonOBJ.isreload) {
                        var index = $('#divright').datagrid('getRowIndex', selected);
                        $('#divright').datagrid('deleteRow', index);
                        $('#divright').datagrid('clearSelections');
                    }
                    alert(jsonOBJ.msg);
                }
            } 
        }
        else
            alert("请先选择一条记录！");
        return false;
    }
</script>
